// ==UserScript==
// @name           Informowanie o postach z dziaów
// @description    Koniec ze spamem ! Otrzymuj informacje o postach z dziaów jakie cię interesują !
// @author         Juri
// @include        http://pl*.plemiona.pl/*
// ==/UserScript==

var strona = document.location.href;
var world=document.location.href.replace("http://","").split('.')[0];

function gup( name , x)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( x );
  if( results == null )
    return "";
  else
    return results[1];
}

var vid=gup("village",window.location.href);

function ustawCookie(nazwa, wartosc, expire) {
        document.cookie = nazwa + "=" + escape(wartosc) + ((expire==null)?"" : ("; expires=" + expire.toGMTString()))
}

function pokazCookie(nazwa) { //1
    if (document.cookie!="") { //2
    var toCookie=document.cookie.split("; ");  //3
        for (i=0; i<toCookie.length; i++) { //4
            var nazwaCookie=toCookie[i].split("=")[0]; //5
            var wartoscCookie=toCookie[i].split("=")[1]; //6
            if (nazwaCookie==nazwa) return unescape(wartoscCookie) //7
        }
    }
}

function insertAfters(newElement,targetElement) {
	//target is what you want it to go after. Look for this elements parent.
	var parent = targetElement.parentNode;
	
	//if the parents lastchild is the targetElement...
	if(parent.lastchild == targetElement) {
		//add the newElement after the target element.
		parent.appendChild(newElement);
		} else {
		// else the target has siblings, insert the new element between the target and it's next sibling.
		parent.insertBefore(newElement, targetElement.nextSibling);
		}
}

function zmien()
{
	var get_dzialy=pokazCookie('dzialy_forum');
	if (get_dzialy != null)
	{
	var dzialy_forum=get_dzialy.split(',');
	}
	var a=document.getElementsByClassName("forum ");
	var b=document.getElementsByClassName("shared_forum ");
	for (n=0;n<a.length;n++)
	{
	oldhtml=a[n].innerHTML;
	var x=a[n].innerHTML.search("forum_id");
	var z=a[n].innerHTML.indexOf('&amp',x);
	var c = a[n].innerHTML.substr(x+9,z-x-9);
	if (get_dzialy != null && dzialy_forum.indexOf(c) != -1) a[n].innerHTML='<input type="checkbox" class="a" id='+n+' checked>'+oldhtml;
	else a[n].innerHTML='<input type="checkbox" class="a" id='+n+'>'+oldhtml;
	}
	for (n=0;n<b.length;n++)
	{
	oldhtml=b[n].innerHTML;
	var x=b[n].innerHTML.search("forum_id");
	var z=b[n].innerHTML.indexOf('&amp',x);
	var c = b[n].innerHTML.substr(x+9,z-x-9);
	if (get_dzialy != null && dzialy_forum.indexOf(c) != -1) b[n].innerHTML='<input type="checkbox" class="b" id='+n+' checked>'+oldhtml;
	else b[n].innerHTML='<input type="checkbox" class="b" id='+n+'>'+oldhtml;
	}
	var div=document.getElementById('dzialy_forum');
	div.innerHTML='<button id="zmiendzialy" type="button">Wybierz działy</button><button id="OK" type="button">OK</button>';
	document.getElementById('OK').addEventListener('click', zmiendzialy, false);
}

function zmiendzialy()
{
	var dzialy="";
	var a=document.getElementsByClassName("a");
	var b=document.getElementsByClassName("b");
	var a2=document.getElementsByClassName("forum ");
	var b2=document.getElementsByClassName("shared_forum ");
	for (n=0;n<a.length;n++)
	{
		if (a[n].checked)
		{
		var x=a2[n].innerHTML.search("forum_id");
	var z=a2[n].innerHTML.indexOf('&amp',x);
	var c = a2[n].innerHTML.substr(x+9,z-x-9);
		dzialy=dzialy+c+',';
		}
	}
	for (n=0;n<b.length;n++)
	{
		if (b[n].checked)
		{
		var x=b2[n].innerHTML.search("forum_id");
	var z=b2[n].innerHTML.indexOf('&amp',x);
	var c = b2[n].innerHTML.substr(x+9,z-x-9);
		dzialy=dzialy+c+',';
		}
	}
	dzialy=dzialy.substr(0,dzialy.length-1);
	var waznosc = new Date()
	waznosc.setMonth(waznosc.getMonth()+6);
	ustawCookie('dzialy_forum',dzialy, waznosc);
	document.location.reload(true);
}

if (strona.search("screen=forum") != -1)
{
	var div=document.getElementById("forum_box");
	var element = document.createElement('div');
	element.id = 'dzialy_forum';
	insertAfters(element,div);
	var div=document.getElementById('dzialy_forum');
	div.innerHTML='<button id="zmiendzialy" type="button">Wybierz działy</button>';
	document.getElementById('zmiendzialy').addEventListener('click', zmien, false);
}

var get_dzialy=pokazCookie('dzialy_forum');
if (get_dzialy != null)
{
var req = new XMLHttpRequest();
req.open('GET', 'http://'+world+'.plemiona.pl/forum.php', true);
req.onreadystatechange=OnStateChange;
req.onload=LoadPosts();
req.send(null);
(function(){
        var timeout = this.window.setInterval(function() {
            req.open('GET', 'http://'+world+'.plemiona.pl/forum.php', true);
			req.onreadystatechange=OnStateChange;
			req.onload=LoadPosts();
            req.send(null);
        }, 7000);
    }).call(this);
}

function LoadPosts()
{
console.log("Load new posts")
}

function OnStateChange () 
{
	if (req.readyState==4) 
	{
	if (req.status==200) 
		{
		txt=req.response;
		var x=txt.search('<div id="forum_box">');
		var y=txt.indexOf("<div>",x);
		var z=txt.indexOf("</div>",y);
		var txt=txt.substr(y,z-y);
		console.log(txt);
		var get_dzialy=pokazCookie('dzialy_forum');
		var dzialy_forum=get_dzialy.split(',');
		for (var n=0;n<dzialy_forum.length;n++)
		{
			var x1=txt.search(dzialy_forum[n])
			var z3=txt.substr(x1+73+dzialy_forum[n].length,9);
			console.log("tekst:"+z3);
			if (z3 == "Nowy wpis")
			{
				var div=document.getElementsByClassName("buttonicon")[0];
				div.innerHTML='<a href="/game.php?village=' + vid + '&amp;screen=ally&amp;mode=forum&amp;check_external=" style="display:inline"><span class="icon header new_post" title="Nowy komentarz w forum"></span></a>';
				console.log("Znaleziono nowy post w dziale : "+dzialy_forum[n]);
				break;
			}
			else
			{
				var div=document.getElementsByClassName("buttonicon")[0];
				div.innerHTML='<a href="/game.php?village=' + vid + '&amp;screen=ally&amp;mode=forum&amp;check_external=" style="display:inline"><span class="icon header no_new_post" title="Na forum plemiennym nie ma nowych postów"></span></a>';
				console.log("Brak nowych postów w dziale : "+dzialy_forum[n]);
			}
		}
		}
		else 
		{
			alert ("Operation failed.");
		}
	}
}